
import 'package:abc/view/time_util.dart';

class VideoResponseData {
  final List<VideoModel> list;
  final int total;
  final int page;
  final int pageSize;

  VideoResponseData({
    required this.list,
    required this.total,
    required this.page,
    required this.pageSize,
  });

  factory VideoResponseData.fromJson(Map<String, dynamic> json) {
    var listItems = json['list'] as List? ?? []; // 默认值为空列表
    List<VideoModel> list =
        listItems.map((i) => VideoModel.fromJson(i)).toList();

    return VideoResponseData(
      list: list,
      total: json['total'] ?? 0, // 默认值为 0
      page: json['page'] ?? 0, // 默认值为 0
      pageSize: json['page_size'] ?? 0,
    );
  }
}

class VideoModel {
  final int id;
  final int isAd; //是否广告 0 否 1 是
  String resourceId;
  final String name;
  String coverImage;
  final String coverImageY;
  final String updateStatus; // 更新状态 1 更新中 2已完结
  final String updateStatusText;
  final String duration;
  final String description;
  String previewMvUrl;
  String mvUrl;
  final int watchNum;
  int likeNum;
  final int realWatchNum;
  final int realLikeNum;
  int commentNum;
  final int collectNum;
  final int shareNum;
  int isLike; // 是否点赞 0-否 1-是
  final List<String> tags;

  // 新增字段
  int discount; // 折扣
  final int level; // 等级
  int payType; // 付费类型 1金币 2 会员 3 免费 4 用户等级
  int price; // 价格
  final AdInfo? adInfo; // 广告信息
  int? contentType; //0视频 1动漫  //1.视频消费 2.视频剧集 3.动漫消费 4.动漫章节 5.小说消费 6.小说章节 7.文章
  int? isBuy; //是否购买
  String? url; //章节url
  String? detailId; //章节url
  String? updated_at; //章节url
  String? created_at; //章节url

  VideoModel({
    required this.id,
    required this.isAd,
    required this.resourceId,
    required this.name,
    required this.coverImage,
    required this.coverImageY,
    required this.updateStatus,
    required this.updateStatusText,
    required this.duration,
    required this.description,
    required this.previewMvUrl,
    required this.mvUrl,
    required this.watchNum,
    required this.likeNum,
    required this.realWatchNum,
    required this.realLikeNum,
    required this.commentNum,
    required this.collectNum,
    required this.shareNum,
    required this.isLike,
    required this.tags,
    required this.discount,
    required this.level,
    required this.payType,
    required this.price,
    required this.contentType,
    this.adInfo,
    this.isBuy,
    this.url,
    this.detailId,
    this.updated_at,
    this.created_at,
  });

  void toggleLike() {
    if (isLike == 0) {
      isLike = 1;
      likeNum += 1;
    } else {
      isLike = 0;
      if (likeNum > 0) {
        likeNum = likeNum - 1;
      }
    }
  }

  bool get getIsLike => isLike == 1;

  String get tagsString {
    String result = tags.map((item) => '#$item').join(' ');
    return result;
  }

  String get formatDuration => TimeUtil.formatVideoDuration(duration);

  ///是否为有效的广告
  bool get isValidAd => isAd == 1 && adInfo != null;

  /// 判断当前视频是否为广告
  /// 判断条件：adInfo不为null，且adInfo.id > 0，且adInfo.adImg不为空
  bool get isAdvertisement {
    return adInfo != null && adInfo!.id > 0 && adInfo!.adImg.isNotEmpty;
  }

  ///是否为动漫
  bool get isAnime => contentType == 1;

  ///动漫是否完结
  bool get isAnimeOver => int.parse(updateStatus) == 2;

  ///自定义key
  String get customKey => '${id}_$resourceId';


  factory VideoModel.fromJson(Map<String, dynamic> json) {
    var tagsFromJson = json['tags'] as List? ?? []; // 默认值为空列表
    List<String> tagsList = tagsFromJson.map((i) => i as String).toList();
    // print('VideoModel.fromJson: tagsList: $tagsList');

    // 处理新的ad_info格式 - 现在是单个对象而不是数组
    AdInfo? adInfoObject;
    if (json['ad_info'] != null) {
      // print('VideoModel.fromJson: ad_info: ${json['ad_info']}');
      adInfoObject = AdInfo.fromJson(json['ad_info']);
      // print('VideoModel.fromJson: adInfoObject: $adInfoObject');
    } else {
      // print('VideoModel.fromJson: ad_info is null');
    }

    return VideoModel(
      id: json['id'] ?? 0,
      // 默认值为 0
      isAd: json['is_ad'] ?? 0,
      // 默认值为 0
      resourceId: json['resource_id'] ?? '',
      // 默认值为空字符串
      name: json['name'] ?? '',
      // 默认值为空字符串
      coverImage: json['cover_image'] ?? '',
      // 默认值为空字符串
      coverImageY: json['cover_imagey'] ?? '',
      // 默认值为空字符串
      updateStatus: json['update_status'] ?? '',
      // 默认值为空字符串
      updateStatusText: json['update_status_text'] ?? '',
      // 默认值为空字符串
      duration: json['duration'] ?? '0',
      // 默认值为 '0'
      description: json['description'] ?? '',
      // 默认值为空字符串
      previewMvUrl: json['preview_mv_url'] ?? '',
      // 默认值为空字符串
      mvUrl: json['mv_url'] ?? '',
      // 默认值为空字符串
      watchNum: json['watch_num'] ?? 0,
      // 默认值为 0
      likeNum: json['like_num'] ?? 0,
      // 默认值为 0
      realWatchNum: json['real_watch_num'] ?? 0,
      // 默认值为 0
      realLikeNum: json['real_like_num'] ?? 0,
      // 默认值为 0
      commentNum: json['comment_num'] ?? 0,
      // 默认值为 0
      collectNum: json['collect_num'] ?? 0,
      // 默认值为 0
      shareNum: json['share_num'] ?? 0,
      // 默认值为 0
      isLike: json['is_like'] ?? 0,
      // 默认值为 1 (未点赞)
      tags: tagsList,
      discount: json['discount'] ?? 0,
      // 默认值为 0
      level: json['level'] ?? 0,
      // 默认值为 0
      payType: json['pay_type'] ?? 0,
      // 默认值为 0
      price: json['price'] ?? 0,
      // 默认值为 0
      adInfo: adInfoObject,
      contentType: json['content_type'] ?? 0,
      isBuy: json['is_buy'] ?? 0,
      url: json['url'] ?? "",
      detailId: json['detail_id'] ?? "",
      updated_at: json['updated_at'] ?? "",
      created_at: json['created_at'] ?? "",
    );
  }

  /// 转换为JSON格式
  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'isAd': isAd,
      'resourceId': resourceId,
      'name': name,
      'coverImage': coverImage,
      'coverImageY': coverImageY,
      'updateStatus': updateStatus,
      'updateStatusText': updateStatusText,
      'duration': duration,
      'description': description,
      'previewMvUrl': previewMvUrl,
      'mvUrl': mvUrl,
      'watchNum': watchNum,
      'likeNum': likeNum,
      'realWatchNum': realWatchNum,
      'realLikeNum': realLikeNum,
      'commentNum': commentNum,
      'collectNum': collectNum,
      'shareNum': shareNum,
      'isLike': isLike,
      'tags': tags,
      'discount': discount,
      'level': level,
      'payType': payType,
      'price': price,
      'adInfo': adInfo?.toJson(),
      'content_type': contentType,
      'isBuy': isBuy,
      'updated_at': updated_at,
      'created_at': created_at,
    };
  }
}

/// 广告信息模型
class AdInfo {
  final int adCategory;
  final String adContent;
  final String adImg;
  final String adLink;
  final String adName;
  final int adPositionId;
  final int adType;
  final int displayDuration;
  final int displayInterval;
  final int displayMenu;
  final int forcedViewTime;
  final int id;
  final int informationFlow;
  final int memberId;
  final String memberName;
  final int redirectType;
  final int revenueType;
  final String skipCondition;
  final int sort;
  final int status;
  final int tenantId;
  final String userLevel;

  AdInfo({
    required this.adCategory,
    required this.adContent,
    required this.adImg,
    required this.adLink,
    required this.adName,
    required this.adPositionId,
    required this.adType,
    required this.displayDuration,
    required this.displayInterval,
    required this.displayMenu,
    required this.forcedViewTime,
    required this.id,
    required this.informationFlow,
    required this.memberId,
    required this.memberName,
    required this.redirectType,
    required this.revenueType,
    required this.skipCondition,
    required this.sort,
    required this.status,
    required this.tenantId,
    required this.userLevel,
  });

  factory AdInfo.fromJson(Map<String, dynamic> json) {
    return AdInfo(
      adCategory: json['ad_category'] ?? 0,
      adContent: json['ad_content'] ?? '',
      adImg: json['ad_img'] ?? '',
      adLink: json['ad_link'] ?? '',
      adName: json['ad_name'] ?? '',
      adPositionId: json['ad_position_id'] ?? 0,
      adType: json['ad_type'] ?? 0,
      displayDuration: json['display_duration'] ?? 0,
      displayInterval: json['display_interval'] ?? 0,
      displayMenu: json['display_menu'] ?? 0,
      forcedViewTime: json['forced_view_time'] ?? 0,
      id: json['id'] ?? 0,
      informationFlow: json['information_flow'] ?? 0,
      memberId: json['member_id'] ?? 0,
      memberName: json['member_name'] ?? '',
      redirectType: json['redirect_type'] ?? 0,
      revenueType: json['revenue_type'] ?? 0,
      skipCondition: json['skip_condition'] ?? '',
      sort: json['sort'] ?? 0,
      status: json['status'] ?? 0,
      tenantId: json['tenant_id'] ?? 0,
      userLevel: json['user_level'] ?? '',
    );
  }

  /// 转换为JSON格式
  Map<String, dynamic> toJson() {
    return {
      'adCategory': adCategory,
      'adContent': adContent,
      'adImg': adImg,
      'adLink': adLink,
      'adName': adName,
      'adPositionId': adPositionId,
      'adType': adType,
      'displayDuration': displayDuration,
      'displayInterval': displayInterval,
      'displayMenu': displayMenu,
      'forcedViewTime': forcedViewTime,
      'id': id,
      'informationFlow': informationFlow,
      'memberId': memberId,
      'memberName': memberName,
      'redirectType': redirectType,
      'revenueType': revenueType,
      'skipCondition': skipCondition,
      'sort': sort,
      'status': status,
      'tenantId': tenantId,
      'userLevel': userLevel,
    };
  }
}
